#include "texture.h"

#include <GL/glu.h>

namespace gfx {

Texture::Texture() {
	glGenTextures(1, &id);
}
Texture::~Texture() {
	glDeleteTextures(1, &id);
}

void Texture::fromImage(SDL_Surface *img) {
	glBindTexture(GL_TEXTURE_2D, id);
	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA,
		img->w, img->h, GL_BGRA, GL_UNSIGNED_BYTE, img->pixels);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR_MIPMAP_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		
	assert(glGetError() == 0);
}

void Texture::bind() {
	glBindTexture(GL_TEXTURE_2D, id);
}

}

